package com.cskaoyan.market.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@WebListener
public class PathListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        ClassLoader classLoader = PathListener.class.getClassLoader();
        Map<String,String> channelMap=new HashMap();
        Map<String,String> pathMap=new HashMap();
        try {
            Properties properties = new Properties();
            properties.load(new InputStreamReader(classLoader.getResourceAsStream("channel.properties"),StandardCharsets.UTF_8));
            for (String stringPropertyName : properties.stringPropertyNames()) {
                channelMap.put(stringPropertyName,properties.getProperty(stringPropertyName));
            }
            properties.clear();
            properties.load(classLoader.getResourceAsStream("path.properties"));
            for (String stringPropertyName : properties.stringPropertyNames()) {
                pathMap.put(stringPropertyName,properties.getProperty(stringPropertyName));
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }



        servletContextEvent.getServletContext().setAttribute("channel",channelMap);
        servletContextEvent.getServletContext().setAttribute("path",pathMap);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
